<?php
// +----------------------------------------------------------------------
// | 区域 模型
// +----------------------------------------------------------------------
namespace app\library\model;

use think\Model;

class Area extends Model{
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = '';
    protected $autoWriteTimestamp = true;
    //修改数据
    public function edit($data,$where){
        $area = new Area();
        $res = $area->save($data,$where);
        if ($res) {
            return true;
        }
        return false;
    }
    /**
     * 获取所有区域(下拉列)
     * @param int $id 选中的ID
     * @return string
     */
    public static function getAreaOption($id,$type = false){
        $rows = self::column('id,pid,areatype,areaname');
        $rows = getTree($rows,0);
        $str = areaOption($rows,$id,$type);
        return $str;

    }
    /**
     * 查询上级是否为分区
     * @param int $pid 
     * @return bool
     */
    public function isBottomPid($pid){
        if ($pid == 0) {
            return false;
        }
        $area = self::get($pid);
        $areatype = $area->areatype;
        if ($areatype == 2) {
            return true;
        }
        return false;
    }
    /**
     * 查询是否有下级
     * @param int $pid 
     * @return bool
     */
    public function isExistSub($id){
        $count = self::where('pid',$id)->count('id');
        if ($count > 0) {
            return true;
        }
        return false;
    } 
    /**
     * 查询id对应的区域名称
     * @param int $id 
     * @return string
     */
    public function get_areaname($id,$str=''){
        $res = self::get($id);
        if (!empty($res->pid)) {
            if (empty($str)) {
                $str = $res->areaname;
            }else{
                $str = $res->areaname .' _ '. $str;
            }
            $str = $this->get_areaname($res->pid,$str);
        }else{
            $str = $res->areaname.' _ '.$str;
        }
        return $str;
    }
    /**
     * 查询区域名称数组
     * @return array
     */
    public function getAllAreaName(){
        $rows = self::column('id,pid,areatype,areaname');
        $rows = getTree($rows,0);
        $arr = areaName($rows);
        return $arr;
    }
    /**
     * 查询id下级id集合
     * @param int $id 
     * @return string
     */
    public function get_child($id){
        $group=[];
        $list = self::where('pid',$id)->column('id');
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $group[$key]['id']=$value;
                $group[$key]['child']=$this->get_child($value);
            }
        }
        return $group;
    }
    /**
     * 删除区域
     * @param array $id 节点ID
     * @return bool
     */
    public function del($ids = '') {
        if (is_array($ids)) {
            $error = '';
            foreach ($ids as $k => $v) {
                $map = [];
                $map['id'] = $v;
                self::where($map)->delete();
            }
            if ($error) {
                $this->error = $error;
                return false;
            }
            return true;
        }
        $this->error = '参数传递错误';
        return false;
    }
    /**
     * 查询某个字段的值
     * @param int $id 
     * @param string $field 字段名
     * @return string
     */
    public function get_id_field($id,$field = 'areaname'){
        $rows = self::get($id);
        return $rows->$field;
    }
}